package c.c.k.service.impl;

import c.c.k.pojo.UserStock;
import c.c.k.repository.UserStockRepository;
import c.c.k.service.UserStockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by chenck on 2022/3/19.
 */
@Service
@Slf4j
public class UserStockServiceImpl implements UserStockService {
    @Autowired
    private UserStockRepository userStockRepository;

    @Override
    public List<UserStock> listByUserId(Long userId) {
        return userStockRepository.findByUserId(userId);
    }

    @Override
    public UserStock getUserStock(Long userId, Long stockId) {
        return userStockRepository.findByUserIdAndStockId(userId, stockId);
    }

    @Override
    public void addUserStock(Long userId, Long stockId) {
        UserStock userStock = userStockRepository.findByUserIdAndStockId(userId, stockId);
        if(userStock != null){
            return;
        }
        userStockRepository.save(UserStock.builder()
                .stockId(stockId)
                .userId(userId)
                .build());
    }

    @Override
    public void delUserStock(Long userId, Long stockId) {
        UserStock userStock = userStockRepository.findByUserIdAndStockId(userId, stockId);
        if(userStock == null){
            return;
        }
        userStockRepository.delete(userStock);
    }
}
